﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Police_Question_ProblemEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bind();
            BindDroptDwon();
        }


    }

    private void Bind()
    {
        Question2DataContext db = new Question2DataContext();

        if (Request["ID"] != null)
        {
            var result = from obj in db.SecurityProblem2 where obj.Id == new Guid(Request["ID"]) select obj;
            SecurityProblem2 problem = result.Single() as SecurityProblem2;
            if (problem == null)
                return;

            if (problem.IsMultiple == 0)
                rbtnSingle.Checked = true;
            else
                rbtnMultiple.Checked = true;

            txtProblem.Text = problem.Title;
            txtGuardCount.Text = problem.GuardCount.Value.ToString();
           

        }


        int Start = Convert.ToInt32('A');
        for (int i = Start; i < Start + 26; i++)
        {
            dptAnswer.Items.Add(new ListItem(Convert.ToChar(i).ToString(), Convert.ToChar(i).ToString()));
        }

        var ff = (from o in db.SecuritySubject2 where o.Id == new Guid(Request["pid"]) select o).FirstOrDefault();
        string DepartmentID = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (ff.CreateDepartment != DepartmentID)
        {
            this.btnAddAnswer.Enabled = false;
            this.btnRemove.Enabled = false;
            this.txtModify.Enabled = false;
            this.btnParent.Enabled = false;


        }
    }
    private void BindDroptDwon()
    {
        lstAnswer.Items.Clear();
        Question2DataContext db = new Question2DataContext();
        var resultAnswer = from an in db.SecurityOption2 where an.PId == new Guid(Request["ID"]) select an;

        foreach (SecurityOption2 o in resultAnswer)
        {
            ListItem item = new ListItem(o.OptionContent, o.Id.ToString());
            if (bool.Parse(o.IsRight.ToString()))
            {
                item.Attributes.CssStyle.Add("Color", "Red");
            }

            lstAnswer.Items.Add(item);

        }
    }
    protected void btnAddAnswer_Click(object sender, EventArgs e)
    {
        Question2DataContext db = new Question2DataContext();
        if (btnAddAnswer.CommandArgument.Trim() == "")
        {
            var result = from obj in db.SecurityOption2 where obj.PId == new Guid(Request["ID"]) select obj;
            SecurityOption2 opt = new SecurityOption2();
            opt.Id = Guid.NewGuid();
            opt.PId = new Guid(Request["ID"]);
            opt.OptionContent = txtAnswer.Text;
            opt.IsRight = ckbRight.Checked;
            opt.Mark = decimal.Parse(txtMark.Text);
            opt.No = dptAnswer.SelectedValue;
            db.SecurityOption2.InsertOnSubmit(opt);
            db.SubmitChanges();

        }
        else
        {
            var result = from obj in db.SecurityOption2 where obj.Id == new Guid(btnAddAnswer.CommandArgument.ToString()) select obj;
            if (result.Count()<1)
                return;

            SecurityOption2 pt = result.Single() as SecurityOption2;
            pt.OptionContent = txtAnswer.Text;
            pt.Mark = decimal.Parse(txtMark.Text);
            pt.IsRight = ckbRight.Checked;
            pt.No = dptAnswer.SelectedValue;
            db.SubmitChanges();
            btnAddAnswer.CommandArgument = "";
            btnAddAnswer.Text = "添加答案";
        }
        BindDroptDwon();
    }
    protected void btnRemove_Click(object sender, EventArgs e)
    {
         Question2DataContext db = new Question2DataContext();
         btnAddAnswer.CommandArgument = "";
         btnAddAnswer.Text = "添加答案";
        foreach (ListItem item in lstAnswer.Items)
        {
            if (item.Selected)
            {
                var result = from obj in db.SecurityOption2 where obj.Id == new Guid(item.Value) select obj;
                db.SecurityOption2.DeleteAllOnSubmit(result);
                db.SubmitChanges();

            }

        }

        BindDroptDwon();
    }
    protected void rbtnSingle_CheckedChanged(object sender, EventArgs e)
    {
        
    }
    protected void txtModify_Click(object sender, EventArgs e)
    {
        try
        {
            Question2DataContext db = new Question2DataContext();

            if (Request["ID"] != null)
            {
                var result = from obj in db.SecurityProblem2 where obj.Id == new Guid(Request["ID"]) select obj;
                SecurityProblem2 problem = result.Single() as SecurityProblem2;
                if (problem == null)
                    return;
                if (rbtnSingle.Checked)
                    problem.IsMultiple = 0;
                else
                    problem.IsMultiple = 1;

                
                problem.Title = txtProblem.Text;
                problem.GuardCount = decimal.Parse(txtGuardCount.Text);
                db.SubmitChanges();
                lblPrompt.Text = "操作成功";


            }
        }
        catch (Exception ex)
        {
            lblPrompt.Text = ex.ToString();
        }
    }
    protected void lstAnswer_SelectedIndexChanged(object sender, EventArgs e)
    {
        Question2DataContext db = new Question2DataContext();

        foreach (ListItem item in lstAnswer.Items)
        {

            if (item.Selected)
            {
                var result = from obj in db.SecurityOption2 where obj.Id == new Guid(item.Value) select obj;

                if (result == null)
                    return;
                SecurityOption2 opt = result.Single() as SecurityOption2;

                txtAnswer.Text = opt.OptionContent;
                ckbRight.Checked = bool.Parse( opt.IsRight.ToString());
                txtMark.Text = opt.Mark.ToString();
                dptAnswer.SelectedValue = opt.No.Trim();
                btnAddAnswer.CommandArgument = opt.Id.ToString();
                btnAddAnswer.Text = "修改答案";


            }
        }

       
    }
    protected void btnParent_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProblemList.aspx?ID="+Request["pid"]);
    }
}
